package ControlLayer;

import java.util.ArrayList;

import DBLayer.DbConnection;
import DBLayer.DbProduct;
import DBLayer.IfDbProduct;
import ModelLayer.Product;
import ModelLayer.Supplier;

public class ProductCtrl
{
	public ArrayList<Product> findAllProducts()
	{
		IfDbProduct dbProduct = new DbProduct();
		ArrayList<Product> allProducts = new ArrayList<Product>();
		allProducts = dbProduct.getAllProducts(false);
		return allProducts;
	}

	public Product findProductByProductNr(int productNr)
	{
		IfDbProduct dbProduct = new DbProduct();
		return dbProduct.findProductByName(productNr, true);
	}

	public int updateProduct(int productNr,String name,Double purchasePrice,Double salesPrice,Double rentPrice,int minStock,int inStock,int size,String colour,String type,String manufacturer,String material,String countryOfOrigin,String description,Supplier supplier)
	{
		IfDbProduct dbProduct = new DbProduct();
		Product product = new Product();
		product.setProductNr(productNr);
		product.setName(name);
		product.setPurchasePrice(purchasePrice);
		product.setSalesPrice(salesPrice);
		product.setRentPrice(rentPrice);
		product.setMinStock(minStock);
		product.setInStock(inStock);
		product.setSize(size);
		product.setColour(colour);
		product.setType(type);
		product.setManufacturer(manufacturer);
		product.setMaterial(material);
		product.setCountryOfOrigin(countryOfOrigin);
		product.setDescription(description);
		product.setSupplier(supplier);
		return dbProduct.updateProduct(product);
	}

	public void insertProduct(int productNr,String name,Double purchasePrice,Double salesPrice,Double rentPrice,int minStock,int inStock,int size,String colour,String type,String manufacturer,String material,String countryOfOrigin,String description,Supplier supplier)
	{
		Product product = new Product();
		product.setProductNr(productNr);
		product.setName(name);
		product.setPurchasePrice(purchasePrice);
		product.setSalesPrice(salesPrice);
		product.setRentPrice(rentPrice);
		product.setMinStock(minStock);
		product.setInStock(inStock);
		product.setSize(size);
		product.setColour(colour);
		product.setType(type);
		product.setManufacturer(manufacturer);
		product.setMaterial(material);
		product.setCountryOfOrigin(countryOfOrigin);
		product.setDescription(description);
		product.setSupplier(supplier);

		try
		{
			DbConnection.startTransaction();
			DbProduct dbProduct = new DbProduct();
			dbProduct.insertProduct(product);
			DbConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DbConnection.rollbackTransaction();
		}
	}

	public int deleteProduct(int productNr)
	{
		IfDbProduct dbProduct = new DbProduct();
		return dbProduct.deleteProduct(productNr);
	}
}